console.log('===',process.env.TARGET);
const path = require('path');
import json from '@rollup/plugin-json'
import ts from 'rollup-plugin-typescript2'
import resolvePlugin from '@rollup/plugin-node-resolve'
import clear from "rollup-plugin-clear" 
const packagesDir = path.resolve(__dirname,'packages')

const target = process.env.TARGET

const packageDir = path.resolve(packagesDir,`${target}`)

const resolve = (p) => path.resolve(packageDir,p)

const pkg = require(resolve('package.json'))

const outputConfig = {
  "esm-bundler":{
    file:resolve(`dist/${target}.esm-bundler.js`),
    format:'es'
  },
  "esm-bundler":{
    file:resolve(`dist/${target}.esm-bundler.js`),
    format:'es'
  },
  "cjs":{
    file:resolve(`dist/${target}.cjs.js`),
    format:'cjs'
  },
  "global":{
    file:resolve(`dist/${target}.global.js`),
    format:'iife'
  },
}

const options = pkg.buildOptions

export default options.formats.map(format => creatConfig(format,outputConfig[format]))

function creatConfig(format,output){
  output.name = options.name
  output.sourcemap = true

  return {
    input:resolve(`src/index.ts`),
    output,
    plugins:[
      json(),
      ts({
        tsconfig:path.resolve(__dirname,'tsconfig.json')
      }),
      resolvePlugin(),
      clear({
        targets:[resolve('dist')]
      })
    ]
  }
}



